package com.vito.dcs.trajectory.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.dcs.asphalt.asphaltDataLog.action.DataAsphaltBlenderLogAction;
import com.vito.dcs.trajectory.model.DataTransactionModel;
import com.vito.dcs.trajectory.model.TransportVehicleModel;
import com.vito.dcs.trajectory.service.DataTransactionService;
import com.vito.dcs.trajectory.service.TransportVehicleService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
/**
 * 
 * 
 * @author zsk
 * @date 2019年4月22日  
 * @version 
 * @Description 车辆运输轨迹接口
 *
 */
@Controller
@RequestMapping("/dcs/sys/dataTransaction")
public class DataTransactionAction extends BaseAction{

	private static final Logger log = LoggerFactory.getLogger(DataTransactionAction.class);

	
	@Reference
	private DataTransactionService dataTransactionServiceImpl;
		
	@Reference
	private TransportVehicleService transportVehicleServiceImpl;
	/**
	 * 车辆运输轨迹坐标数据录入接口（离线数据录入）
	 * @return
	 */
	@ResponseBody
	@RequestMapping("devicePushPackage")
	public Result devicePushPackage(@RequestBody String params){
		Result rs = new Result();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//预留后期解析离线文件，进行数据获取
		
		if(params.toString() != null && !"".equals(params)){
			try{
				//测试数据模拟
				String jsonTest = "{'type': 'TPJ','mac': '5b22-cd1d-1a11-54bc','lat': 104.33,'lng': 50.07805,'height': 19.636159922457583,'val_1': 'null','val_2': '18.554724','val_3': 'error','val_4': '22','val_5': '55','val_6': '66','val_7': '77','val_8': '88','val_9': '99','ios': 111.21,'time': '2017-11-08 10:29:24.321','isSyc': 1,'sat_num': 11,'dif_data': 0,'data': 1,'seq': '009121212','A1': 4.35,'A2': ,2.00,'A3': 0.92,'Gx': -0.161,'Gy': 0.006,'Gz': 1.863}";
				System.out.println(params.toString());
				JSONObject jsonObject = JSONObject.parseObject(params);
				Map<String,Object> map = jsonObject;
				//调用保存方法进行数据存储
				DataTransactionModel dataTransactionModel = new DataTransactionModel();
				//根据Mac 查询对应车辆ID进行存储
				if(map.get("mac") != null){
					//获取mac设备对应的车辆ID
					TransportVehicleModel transportVehicleModel =  transportVehicleServiceImpl.findTransportVehicle(map.get("mac").toString());
					if(transportVehicleModel != null){
						dataTransactionModel.setIsHandler(transportVehicleModel.getId());
					}
					dataTransactionModel.setMac(map.get("mac").toString());
				}
				if(map.get("height") != null){
					dataTransactionModel.setHeight(Double.parseDouble(map.get("height").toString()));
				}
				if(map.get("lat") != null){
					dataTransactionModel.setLat(Double.parseDouble(map.get("lat").toString()));
				}
				if(map.get("lng") != null){
					dataTransactionModel.setLng(Double.parseDouble(map.get("lng").toString()));
				}
				if(map.get("time") != null){
					dataTransactionModel.setTime(sdf.parse(map.get("time").toString()));
				}
				if(map.get("weight") != null){
					dataTransactionModel.setWeight(Double.parseDouble(map.get("weight").toString()));	
				}
				dataTransactionServiceImpl.saveDataTransaction(dataTransactionModel);
				rs.setMsg("保存成功");
				rs.setCode(Result.CODE_OK);
				rs.setMsg(ResMsg.SAVESUC);
			} catch (Exception e) {
				log.error("保存失败",e);
				rs.setCode(Result.CODE_ERROR);
				rs.setMsg(ResMsg.EXECERR);
			}
		    return rs;
		}else{
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg("程序执行异常");
			return rs;
		}
	}
	/**
	 * 
	 * 
	 * @author zsk
	 * @date 2019年4月23日  
	 * @version 
	 * @Description  分页查询获取车辆轨迹记录列表
	 *
	 */
	public Result queryDataTransactionList(){
		Result result = new Result();
		
		
		return result;
	}
}
